package com.diy.sigmund.diyorder.test.controller;

import com.diy.sigmund.diycommon.entity.base.exception.DiyRuntimeException;
import com.diy.sigmund.diycommon.entity.enumerate.ResponseEnum;
import com.diy.sigmund.diyorder.test.entity.UserDto;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ylm-sigmund
 * @since 2021/5/4 12:13
 */
@RestController
@RequestMapping(value = "/user")
public class UserRestController {

    @PostMapping("/insert")
    public boolean insert(@RequestBody UserDto user) {
        System.out.println("开始新增...");
        //如果姓名为空就手动抛出一个自定义的异常！
        if (user.getName() == null) {
            throw new DiyRuntimeException(ResponseEnum.USER_NAME_ERROR);
        }
        return true;
    }

    @PostMapping("/update")
    public boolean update(@RequestBody UserDto user) {
        System.out.println("开始更新...");
        //这里故意造成一个空指针的异常，并且不进行处理
        String str = null;
        str.equals("111");
        return true;
    }

    @PostMapping("/delete")
    public boolean delete(@RequestBody UserDto user) {
        System.out.println("开始删除...");
        //这里故意造成一个异常，并且不进行处理
        Integer.parseInt("abc123");
        return true;
    }

    @PostMapping("/findByUser")
    public List<UserDto> findByUser(UserDto user) {
        System.out.println("开始查询...");
        List<UserDto> userList = new ArrayList<>();
        UserDto user2 = new UserDto();
        user2.setId(1);
        user2.setName("xuwujing");
        user2.setAge(18);
        userList.add(user2);
        return userList;
    }

}
